package com.caigou.modules.personal.adapter

import android.graphics.Color
import android.text.TextUtils
import android.util.TypedValue
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.caigou.comm.base.ext.GlideDisplay
import com.caigou.comm.base.utils.ImageLoader
import com.caigou.modules.personal.R
import com.caigou.modules.personal.bean.PostItemBean
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lwkandroid.widget.ngv.DefaultNgvAdapter
import com.lwkandroid.widget.ngv.NineGridView

/**
 * @author #Suyghur.
 * Created on 2021/11/20
 */
class TicketItemAdapter(dataset: MutableList<PostItemBean>) :
    BaseQuickAdapter<PostItemBean, BaseViewHolder>(R.layout.item_page_ticket_list, dataset), LoadMoreModule {

    override fun convert(holder: BaseViewHolder, item: PostItemBean) {
        if (TextUtils.isEmpty(item.headImg)) {
            holder.setImageResource(R.id.item_page_ticket_iv_avatar, R.drawable.img_comm_avatar)
        } else {
            ImageLoader.loadCircle(context, item.headImg, holder.getView(R.id.item_page_ticket_iv_avatar))
        }

        if (item.essence == 1) {
            holder.getView<ImageView>(R.id.item_page_ticket_tv_start).visibility = View.VISIBLE
            ImageLoader.load(context, item.essenceIcon, holder.getView(R.id.item_page_ticket_tv_start))
        } else {
            holder.getView<ImageView>(R.id.item_page_ticket_tv_start).visibility = View.GONE
        }

        if (item.isDare == 1) {
            holder.getView<TextView>(R.id.item_page_ticket_tv_dare).visibility = View.VISIBLE
        } else {
            holder.getView<TextView>(R.id.item_page_ticket_tv_dare).visibility = View.GONE
        }

        holder.setText(R.id.item_page_ticket_tv_nickname, item.nickName)
        if (item.vip == 1) {
            holder.getView<TextView>(R.id.item_page_ticket_tv_nickname).setTextColor(Color.parseColor("#FE6880"))
        } else {
            holder.getView<TextView>(R.id.item_page_ticket_tv_nickname).setTextColor(Color.parseColor("#505050"))
        }

        holder.setText(R.id.item_page_ticket_tv_time, item.createTime)

        if (item.type == 5) {
            holder.setText(R.id.item_page_ticket_tv_period, item.fifaTypeName)
            holder.getView<TextView>(R.id.item_page_ticket_tv_period).setBackgroundColor(Color.parseColor("#6C99E9"))

        } else {
            holder.setText(R.id.item_page_ticket_tv_period, item.period.toString())
            if (item.currPeriod) {
                holder.getView<TextView>(R.id.item_page_ticket_tv_period).setBackgroundColor(Color.parseColor("#6C99E9"))
            } else {
                holder.getView<TextView>(R.id.item_page_ticket_tv_period).setBackgroundColor(Color.parseColor("#B9B9B9"))
            }
        }

        holder.setText(R.id.item_page_ticket_tv_title, item.title)
        holder.setText(R.id.item_page_ticket_tv_content, item.content)

        holder.getView<NineGridView>(R.id.item_page_ticket_ngv_display).run {
            if (item.imgs.size > 0) {
                val imgs = if (item.imgs.size > 3) {
                    item.imgs.slice(1..3)
                } else {
                    item.imgs
                }
                visibility = View.VISIBLE
                setSingleImageSize(TypedValue.COMPLEX_UNIT_DIP, 150, 200)
                val ngvAdapter = DefaultNgvAdapter(100, GlideDisplay())
                ngvAdapter.dataList = imgs
                adapter = ngvAdapter
            } else {
                visibility = View.GONE
            }
        }

        holder.setText(R.id.item_page_ticket_tv_like_num, item.nicePointCount.toString())
        if (item.like) {
            holder.getView<TextView>(R.id.item_page_ticket_tv_like_num).setTextColor(Color.parseColor("#FE6880"))
            holder.setImageResource(R.id.item_page_ticket_iv_like, R.drawable.img_personal_like)
        } else {
            holder.getView<TextView>(R.id.item_page_ticket_tv_like_num).setTextColor(Color.parseColor("#B9B9B9"))
            holder.setImageResource(R.id.item_page_ticket_iv_like, R.drawable.img_personal_unlike)
        }

        holder.setText(R.id.item_page_ticket_tv_browse_num, item.browseCount.toString())
        holder.setText(R.id.item_page_ticket_tv_comment_num, item.replyCount.toString())
    }
}